const crypto = require("crypto")
const {userInsert, searchUser} = require("../model/user")



const secret = 'itissecret'
const register = (req,res)=>{
    let {userName, pwd} = req.body
    console.log()
    //查询是否存在
    searchUser({userName}).then(user=>{
        if(user.length>0){
            res.json({
                code:400,
                data:{
                    msg:"用户名已存在"
                }
            })
        }else{
        //加密
        pwd  = crypto.createHmac('sha256',secret).update(pwd).digest('hex')
        console.log(pwd)
        userInsert({userName,pwd}).then(user =>{
            res.json({
                code:200,
                data:{
                    msg:"注册成功",
                    user:user.userName
                }
            })
        })
    }
    })
}

module.exports = {
    register
}